package com.rustic.server.controller.api;

import cn.hutool.core.collection.CollUtil;
import cn.xy.base.server.bean.IPage;
import cn.xy.base.server.utils.PageUtil;
import com.rustic.server.dto.InteractionDataDTO;
import com.rustic.server.po.UserLikeLink;
import com.rustic.server.query.UserLikeLinkUserListQuery;
import com.rustic.server.service.UserLikeLinkService;
import com.rustic.server.vo.list.UserLikeLinkWebListVO;
import com.rustic.server.vo.UserLikeLinkVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rustic.server.web.WebContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[user_like_link]对应的前台接口
 *
 * @author auto
 */

@Slf4j
@RestController
@RequestMapping("/api/userLikeLink")
@Api(tags = {"用户点赞关联接口"})
public class UserLikeLinkController {

    @Autowired
    private UserLikeLinkService userLikeLinkService;

    @ApiOperation(value = "用户点赞关联-列表")
    @GetMapping("/list")
    public Result<Page<UserLikeLinkWebListVO>> list(UserLikeLinkUserListQuery query) {
        int userId = WebContext.getUserId();
        LambdaQueryWrapper<UserLikeLink> queryWrapper = userLikeLinkService.toWebListWrapper(query, userId);
        Page<UserLikeLink> pageData = userLikeLinkService.page(query.toPage(), queryWrapper);
        List<UserLikeLink> userLikeLinkList = pageData.getRecords();
        if (CollUtil.isEmpty(userLikeLinkList)) {
            return Result.data(new Page<>());
        }
        List<UserLikeLinkWebListVO> results = userLikeLinkService.toWebListVo(userLikeLinkList);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "用户点赞关联-详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户点赞关联id"),
    })
    @GetMapping("/detail")
    public Result<UserLikeLinkVO> detail(int id) {
        if (id <= 0) {
            return Result.error(UserLikeLinkService.USER_LIKE_LINK_NOT_EXIST);
        }
        UserLikeLink userLikeLink = userLikeLinkService.getById(id);
        if (userLikeLink == null) {
            return Result.error(UserLikeLinkService.USER_LIKE_LINK_NOT_EXIST);
        }
        UserLikeLinkVO userLikeLinkVO = new UserLikeLinkVO(userLikeLink);
        return Result.data(userLikeLinkVO);
    }

    @ApiOperation(value = "用户点赞关联-添加取消")
    @PostMapping("/addOrDel")
    public Result<String> addOrDel(@RequestBody @Validated InteractionDataDTO interaction) {
        int userId = WebContext.getUserId();
        String addOrDelLike = userLikeLinkService.addOrDelLike(userId, interaction.getTargetId(), interaction.getType());
        return Result.ok(addOrDelLike);
    }

}
